// post.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        select: 0,
        countdown: 60,
        VerifyCode: '验证码',
        total_micro_second: 60 * 1000,
        text_title: '请选择',
        text_titletwo: '请选择',
        mobile: '',
        index: '',
        vcode: '',
        submitdata: { req: '', industry: '', budget: '', desc: '', way: '', name: '', category: '' }

    },

    formSubmit: function(e) {
        console.log('form发生了submit事件，携带数据为：', e.detail.value)

    },

    onShareAppMessage: function() {

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let mtype = "";
        if (options.index == "1") {
            mtype = "req"
            wx.setNavigationBarTitle({
                title: "发需求" //页面标题为路由参数
            })
        } else {
            mtype = "wxapp"
            wx.setNavigationBarTitle({
                title: "小程序" //页面标题为路由参数
            })
        }

        this.setData({
            index: options.index,
            'submitdata.category': mtype
        })


    },
    wantclick: function(e) {
        var index = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../Multiselect/multiselect?index=' + index + "&finalindex=" + this.data.index + "&text_title=" + this.data.text_title +
                "&text_titletwo=" + this.data.text_titletwo
        })
    },
    mchange: function(e) {
        let value = e.detail.value;
        this.setData({
            mobile: value
        })
    },
    codechange: function(e) {
        let codedata = e.detail.value;
        this.setData({
            vcode: codedata
        })
    },

    wantchange: function(e) {

        let wantdata = e.detail.value;
        this.setData({
            'submitdata.req': wantdata
        })
    },
    valuechange: function({ text_title, text_titletwo }) {
        if (text_title) {
            this.setData({
                'submitdata.req': text_title,
                'text_title': text_title
            })
        }
        if (text_titletwo) {
            this.setData({
                'submitdata.industry': text_titletwo,
                'text_titletwo': text_titletwo
            })
        }

    },
    jobchange: function(e) {
        let jobdata = e.detail.value;
        this.setData({
            'submitdata.industry': jobdata
        })
    },
    budgetchange: function(e) {
        let budgetdata = e.detail.value
        this.setData({
            'submitdata.budget': budgetdata
        })
    },

    descChange: function(e) {
        let desc = e.detail.value
        this.setData({
            'submitdata.desc': desc
        })
    },
    namechange: function(e) {
        let name = e.detail.value
        this.setData({
            'submitdata.name': name
        })
    },
    submit: function() {
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; //上一个页面

        var that = this;
        if (this.data.select == 1) {
            this.setData({
                'submitdata.way': '我要定制'
            })
        } else if (this.data.select == 2) {
            this.setData({
                'submitdata.way': '选择标品'
            })
        }

        app.util.request({
            'url': 'entry/wxapp/form',
            data: {
                m: 'llt_xappsc',
                mobile: that.data.mobile,
                code: that.data.vcode,
                data: this.data.submitdata
            },
            'cachetime': '30',
            success: function(res) {
                wx.showToast({
                    title: '成功提交',
                    icon: 'succes',
                    duration: 1500,
                    mask: true,
                    complete: function() {
                        setTimeout(wx.navigateBack({
                            delta: 1
                        }), 5000)
                    }
                })


            }
        });

    },
    left_way: function() {
        if (this.data.select != 1) {
            this.setData({ select: 1 })
        } else {
            this.setData({ select: 0 })
        }

    },
    right_way: function() {
        if (this.data.select != 2) {
            this.setData({ select: 2 })
        } else {
            this.setData({ select: 0 })
        }
    },
    call_customer: function() {
        var pages = getCurrentPages();

        var prevPage = pages[pages.length - 2]; //上一个页面
        wx.makePhoneCall({
            phoneNumber: prevPage.data.mobile,
            success: function() {
                console.log("拨打电话成功！")
            },
            fail: function() {
                console.log("拨打电话失败！")
            }
        })
    },
    wantto: function(e) {
        var index = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../Multiselect/multiselect?index=' + index + "&finalindex=" + this.data.index + "&text_title=" + this.data.text_title +
                "&text_titletwo=" + this.data.text_titletwo
        })
    },


    setVerify(e) { //发送验证码
        var that = this;
        app.util.request({
            'url': 'entry/wxapp/sms',
            data: {
                m: 'llt_xappsc',
                mobile: that.data.mobile
            },
            'cachetime': '30',
            success: function(res) {
                console.log(res)

                //验证码倒计时
                if (that.data.total_micro_second <= 0 || that.data.total_micro_second == 60 * 1000) {
                    if (that.data.VerifyCode == "重新发送") {
                        that.setData({ total_micro_second: 60 * 1000 })
                    }
                    that.count_down();
                }
            }
        });
    },
    count_down() {
        if (this.data.total_micro_second <= 0) {
            this.setData({
                VerifyCode: "重新发送"
            });
            // timeout则跳出递归
            return;
        }
        // 渲染倒计时时钟
        this.setData({
            VerifyCode: date_format(this.data.total_micro_second) + " 秒"
        });

        var that = this;
        setTimeout(function() {
            // 放在最后--
            that.data.total_micro_second -= 10;

            that.count_down();


        }, 10)
    }
})



// 时间格式化输出，如03:25:19 86。每10ms都会调用一次
function date_format(micro_second) {
    // 秒数
    var second = Math.floor(micro_second / 1000);
    // 小时位
    var hr = Math.floor(second / 3600);
    // 分钟位
    var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
    // 秒位
    var sec = fill_zero_prefix((second - hr * 3600 - min * 60)); // equal to => var sec = second % 60;
    // 毫秒位，保留2位
    var micro_sec = fill_zero_prefix(Math.floor((micro_second % 1000) / 10));
    return sec;
}
// 位数不足补零
function fill_zero_prefix(num) {
    return num < 10 ? "0" + num : num
}